package ae.pcfc.cldos.online.web.controller.error;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BaseController;


@Controller
public class ErrorController extends BaseController {

	@RequestMapping(ViewPath.__ERROR)
	public ModelAndView generic(ModelMap model){
		Throwable e = requestVariablesBean.getExceptionStore();
		String stackTrace = "";
		if(e!=null){
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			stackTrace = sw.toString();
			requestVariablesBean.setExceptionStore(null);
		}
		
		
		model.put("errormessage", stackTrace);
		return new ModelAndView("error.generic");
	}
	
	@RequestMapping(ViewPath.__NOT_FOUND)
	public ModelAndView notFound(HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		return new ModelAndView("404layout");
	}
	
	@RequestMapping(ViewPath.__ACCESS_DENIDED)
	public ModelAndView notFound() throws IOException{
		return new ModelAndView("denidedLayout");
	}
	
}
